package com.cakin;

import java.sql.*;

/**
 * @ClassName: JdbcTest
 * @Description: Jdbc测试
 * @Date: 2020/6/18
 * @Author: cakin
 */
public class JdbcTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // useUnicode=true&characterEncoding=utf8 解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8";
        String username = "root";
        String password = "123456";

        // 1 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2 获得连接对象
        Connection connection = DriverManager.getConnection(url, username, password);
        // 3 获得发送SQL对象statement，可以进行增删查改操作
        Statement statement = connection.createStatement();
        // 4 编写SQL
        String sql = "select * from users";
        // 5 执行SQL
        ResultSet rs = statement.executeQuery(sql);

        while (rs.next()) {
            System.out.println("id=" + rs.getObject("id"));
            System.out.println("name=" + rs.getObject("name"));
            System.out.println("password=" + rs.getObject("password"));
            System.out.println("email=" + rs.getObject("email"));
        }
        // 6 关闭资源
        rs.close();
        statement.close();
        connection.close();
    }
}
